import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sToa on 2014/12/24.
 */
public class SAXParserHelper extends DefaultHandler {

    private List<Book> books;
    private Book book = null;
    private boolean nb = false;
    private boolean tb = false;
    private boolean pb = false;

    public List<Book> getBooks() {
        return books;
    }

    @Override
    public void startDocument() throws SAXException {
        System.out.println("Start parsing document...");
        books = new ArrayList<Book>();
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("End");
    }

    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes atts) throws SAXException {
        // Using qualified name because we are not using xmlns prefixes here.
        System.out.println("startElement");
        System.out.println("uri: " + uri + " localName: " + localName + " qName: " + qName + " atts: " + atts.getLocalName(0));
        if (qName.equals("book")) {
            book = new Book();
        }
        if (qName.equals("name")) {
            nb = true;
        }
        if (qName.equals("title")) {
            tb = true;
        }
        if (qName.equals("price")) {
            pb = true;
        }

    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName)
            throws SAXException {
        // End of processing current element
        System.out.println("endElement");
        System.out.println("namespaceURI: " + namespaceURI + " localName: " + localName + " qName: " + qName);
        if (qName.equals("book")) {
            books.add(book);
            book = null;
        }
        if (qName.equals("name")) {
            nb = false;
        }
        if (qName.equals("title")) {
            tb = false;
        }
        if (qName.equals("price")) {
            pb = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) {
        // Processing character data inside an element
        String attr = new String(ch, start, length);
        if(null != book)
        {
            if (nb) {
                book.setName(attr);
            }
            if (tb) {
                book.setTitle(attr);
            }
            if (pb) {
                book.setPrice(Long.parseLong(attr));
            }
        }
    }

    public static void main(String[] args) throws SAXException, IOException {
        XMLReader parser = XMLReaderFactory.createXMLReader();
        SAXParserHelper bookHandler = new SAXParserHelper();
        parser.setContentHandler(bookHandler);
        parser.parse("D:\\WorkSpace-IDE\\parser\\src\\main\\resources\\xml\\ArubaPolicyManagerParser.xml");
        for(Book book: bookHandler.getBooks())
        {
            System.out.println("书名：" + book.getName() +" 标题：" + book.getTitle() + " 价格：" + book.getPrice());
        }
    }

}
